<?php
/**
 * @author kongkai <kongkai@88.com>
 * @date 2022-04-17
 */

namespace app\app\controller;

use app\app\model\ConstructionDrawingsFileModel;
use app\app\model\RandModel;
use think\db\exception\DbException;

class ConstructionDrawingsFile extends BaseCurd
{
    private $model;

    /**
     * initialize
     * @return void
     */
    public function initialize()
    {
        $this->model = ConstructionDrawingsFileModel::class;
    }

    /**
     * C&U
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new $this->model();
            $model->id = RandModel::randId();
        } else {
            $model = $this->model::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }

    /**
     * R
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $where = $this->request->param();
        unset($where['page'], $where['pageSize']);
        $where = array_filter($where);

        $page_size = input('pageSize', '20');

        $tmp = [];
        foreach ($where as $k => $v) {
            $tmp['a.' . $k] = $v;
        }
        $where = $tmp;
        unset($tmp);

        $data = $this->model::alias('a')
            ->leftJoin('sys_file_info b', 'b.file_id = a.file_id')
            ->where($where)
            ->field('a.*, b.file_path')
            ->paginate($page_size);
        $data = $data->toArray();

        empty($data) && $this->error('查询为空');
        empty($data['data']) && $this->error('未找到符合条件的资源');

        $this->success('success', $data);
    }

    /**
     * detail
     * @param $id
     * @return void
     */
    public function detail($id = 0)
    {
        !$id && $this->error('请传入id');

        $data = $this->model::where('id', $id)->find();
        empty($data) && $this->error('查询为空');

        $this->success('success', $data);
    }

    /**
     * D
     * @return void
     * @throws DbException
     */
    public function del($id = 0)
    {
        !$id && $this->error('请传入id');

        $this->model::where('id', $id)->delete();

        $this->success('success');
    }
}